home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Frame.java < prev    next >
Text File  |  1998-09-22  |  12KB  |  458 lines

  1. /*
  2.  * @(#)Frame.java    1.77 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.FramePeer;
  17. import java.awt.event.*;
  18. import java.util.Vector;
  19. import java.io.ObjectOutputStream;
  20. import java.io.ObjectInputStream;
  21. import java.io.IOException;
  22.  
  23.  
  24. /**
  25.  * A Frame is a top-level window with a title and a border.
  26.  * The default layout for a frame is BorderLayout.
  27.  *
  28.  * Frames are capable of generating the following types of window events:
  29.  * WindowOpened, WindowClosing, WindowClosed, WindowIconified,
  30.  * WindowDeiconified, WindowActivated, WindowDeactivated.
  31.  *
  32.  * @version     1.77, 07/01/98
  33.  * @author     Sami Shaio
  34.  * @see WindowEvent
  35.  * @see Window#addWindowListener
  36.  * @since       JDK1.0
  37.  */
  38. public class Frame extends Window implements MenuContainer {
  39.  
  40.     /* Note: These are being obsoleted;  programs should use the Cursor class
  41.      * variables going forward. See Cursor and Component.setCursor.
  42.      */
  43.  
  44.    /**
  45.     * 
  46.     */
  47.     public static final int    DEFAULT_CURSOR           = Cursor.DEFAULT_CURSOR;
  48.  
  49.  
  50.    /**
  51.     *
  52.     */
  53.     public static final int    CROSSHAIR_CURSOR         = Cursor.CROSSHAIR_CURSOR;
  54.  
  55.    /**
  56.     * 
  57.     */
  58.     public static final int    TEXT_CURSOR              = Cursor.TEXT_CURSOR;
  59.  
  60.    /**
  61.     * 
  62.     */
  63.     public static final int    WAIT_CURSOR             = Cursor.WAIT_CURSOR;
  64.  
  65.    /**
  66.     * 
  67.     */
  68.     public static final int    SW_RESIZE_CURSOR         = Cursor.SW_RESIZE_CURSOR;
  69.  
  70.    /**
  71.     * 
  72.     */
  73.     public static final int    SE_RESIZE_CURSOR         = Cursor.SE_RESIZE_CURSOR;
  74.  
  75.    /**
  76.     * 
  77.     */
  78.     public static final int    NW_RESIZE_CURSOR        = Cursor.NW_RESIZE_CURSOR;
  79.  
  80.    /**
  81.     * 
  82.     */
  83.     public static final int    NE_RESIZE_CURSOR         = Cursor.NE_RESIZE_CURSOR;
  84.  
  85.    /**
  86.     * 
  87.     */
  88.     public static final int    N_RESIZE_CURSOR         = Cursor.N_RESIZE_CURSOR;
  89.  
  90.    /**
  91.     *
  92.     */
  93.     public static final int    S_RESIZE_CURSOR         = Cursor.S_RESIZE_CURSOR;
  94.  
  95.    /**
  96.     * 
  97.     */
  98.     public static final int    W_RESIZE_CURSOR             = Cursor.W_RESIZE_CURSOR;
  99.  
  100.    /**
  101.     * 
  102.     */
  103.     public static final int    E_RESIZE_CURSOR            = Cursor.E_RESIZE_CURSOR;
  104.  
  105.    /**
  106.     * 
  107.     */
  108.     public static final int    HAND_CURSOR            = Cursor.HAND_CURSOR;
  109.  
  110.    /**
  111.     * 
  112.     */
  113.     public static final int    MOVE_CURSOR            = Cursor.MOVE_CURSOR;    
  114.  
  115.     String     title = "Untitled";
  116.     Image      icon;
  117.     MenuBar    menuBar;
  118.     boolean    resizable = true;
  119.     boolean     mbManagement = false;   /* used only by the Motif impl. */
  120.  
  121.     /* 
  122.      * The Windows owned by the Frame.
  123.      */
  124.     Vector ownedWindows;
  125.  
  126.     private static final String base = "frame";
  127.     private static int nameCounter = 0;
  128.  
  129.     /*
  130.      * JDK 1.1 serialVersionUID 
  131.      */
  132.      private static final long serialVersionUID = 2673458971256075116L;
  133.  
  134.     /**
  135.      * Constructs a new instance of <code>Frame</code> that is 
  136.      * initially invisible.
  137.      * @see Component#setSize
  138.      * @see Component#setVisible
  139.      * @since JDK1.0
  140.      */
  141.     public Frame() {
  142.         this("");
  143.     }
  144.  
  145.     /**
  146.      * Constructs a new, initially invisible <code>Frame</code> object 
  147.      * with the specified title.
  148.      * @param title the title for the frame
  149.      * @see java.awt.Component#setSize
  150.      * @see java.awt.Component#setVisible
  151.      * @since JDK1.0
  152.      */
  153.     public Frame(String title) {
  154.     this.title = title;
  155.     visible = false;
  156.     setLayout(new BorderLayout());
  157.     }
  158.  
  159.     /**
  160.      * Construct a name for this component.  Called by getName() when the
  161.      * name is null.
  162.      */
  163.     String constructComponentName() {
  164.         return base + nameCounter++;
  165.     }
  166.  
  167.     /** 
  168.      * Adds the specified window to the list of windows owned by
  169.      * the frame.
  170.      * @param window the window to be added
  171.      */
  172.     Window addOwnedWindow(Window window) {
  173.         if (window != null) {
  174.         if (ownedWindows == null) {
  175.             ownedWindows = new Vector();
  176.         }
  177.         ownedWindows.addElement(window);
  178.     }
  179.     return window;
  180.     }
  181.  
  182.     /** 
  183.      * Removes the specified window from the list of windows owned by
  184.      * the frame.
  185.      * @param window the window to be added
  186.      */
  187.     void removeOwnedWindow(Window window) {
  188.         if (window != null) {
  189.         if (ownedWindows != null) {
  190.           ownedWindows.removeElement(window);
  191.         }
  192.     }
  193.     }
  194.  
  195.     /**
  196.      * Creates the Frame's peer.  The peer allows us to change the look
  197.      * of the Frame without changing its functionality.
  198.      * @since JDK1.0
  199.      */
  200.     public void addNotify() {
  201.         synchronized (getTreeLock()) {
  202.         if (peer == null)
  203.             peer = getToolkit().createFrame(this);
  204.             MenuBar menuBar = this.menuBar;
  205.         if (menuBar != null) {
  206.             menuBar.addNotify();
  207.             ((FramePeer)peer).setMenuBar(menuBar);
  208.         }
  209.         super.addNotify();
  210.         }
  211.     }
  212.  
  213.     /**
  214.      * Gets the title of the frame.
  215.      * @return    the title of this frame, or <code>null</code> 
  216.      *                if this frame doesn't have a title.
  217.      * @see       java.awt.Frame#setTitle
  218.      * @since     JDK1.0
  219.      */
  220.     public String getTitle() {
  221.     return title;
  222.     }
  223.  
  224.     /**
  225.      * Sets the title for this frame to the specified title.
  226.      * @param    title    the specified title of this frame.
  227.      * @see      java.awt.Frame#getTitle
  228.      * @since    JDK1.0
  229.      */
  230.     public synchronized void setTitle(String title) {
  231.     this.title = title;
  232.         FramePeer peer = (FramePeer)this.peer;
  233.     if (peer != null) {
  234.         peer.setTitle(title);
  235.     }
  236.     }
  237.  
  238.     /**
  239.      * Gets the icon image for this frame.
  240.      * @return    the icon image for this frame, or <code>null</code> 
  241.      *                    if this frame doesn't have an icon image.
  242.      * @see       java.awt.Frame#setIconImage
  243.      * @since     JDK1.0
  244.      */
  245.     public Image getIconImage() {
  246.     return icon;
  247.     }
  248.  
  249.     /**
  250.      * Sets the image to display when this frame is iconized. 
  251.      * Not all platforms support the concept of iconizing a window.
  252.      * @param     image the icon image to be displayed
  253.      * @see       java.awt.Frame#getIconImage
  254.      * @since     JDK1.0
  255.      */
  256.     public synchronized void setIconImage(Image image) {
  257.     this.icon = image;
  258.         FramePeer peer = (FramePeer)this.peer;
  259.     if (peer != null) {
  260.         peer.setIconImage(image);
  261.     }
  262.     }
  263.  
  264.     /**
  265.      * Gets the menu bar for this frame.
  266.      * @return    the menu bar for this frame, or <code>null</code> 
  267.      *                   if this frame doesn't have a menu bar.
  268.      * @see       java.awt.Frame#setMenuBar
  269.      * @since     JDK1.0
  270.      */
  271.     public MenuBar getMenuBar() {
  272.     return menuBar;
  273.     }
  274.  
  275.     /**
  276.      * Sets the menu bar for this frame to the specified menu bar.
  277.      * @param     mb the menu bar being set
  278.      * @see       java.awt.Frame#getMenuBar
  279.      * @since     JDK1.0
  280.      */
  281.     public void setMenuBar(MenuBar mb) {
  282.         synchronized (getTreeLock()) {
  283.         if (menuBar == mb) {
  284.             return;
  285.         }
  286.         if ((mb != null) && (mb.parent != null)) {
  287.             mb.parent.remove(mb);
  288.         }
  289.         if (menuBar != null) {
  290.             remove(menuBar);
  291.         }
  292.         menuBar = mb;
  293.         if (menuBar != null) {
  294.             menuBar.parent = this;
  295.  
  296.             FramePeer peer = (FramePeer)this.peer;
  297.             if (peer != null) {
  298.             mbManagement = true;
  299.             menuBar.addNotify();
  300.             peer.setMenuBar(menuBar);
  301.             }
  302.         }
  303.             invalidate();
  304.         }
  305.     }
  306.  
  307.     /**
  308.      * Indicates whether this frame is resizable.  
  309.      * By default, all frames are initially resizable. 
  310.      * @return    <code>true</code> if the user can resize this frame; 
  311.      *                        <code>false</code> otherwise.
  312.      * @see       java.awt.Frame#setResizable
  313.      * @since     JDK1.0
  314.      */
  315.     public boolean isResizable() {
  316.     return resizable;
  317.     }
  318.  
  319.     /**
  320.      * Sets the resizable flag, which determines whether 
  321.      * this frame is resizable. 
  322.      * By default, all frames are initially resizable. 
  323.      * @param    resizable   <code>true</code> if this frame is resizable; 
  324.      *                       <code>false</code> otherwise.
  325.      * @see      java.awt.Frame#isResizable
  326.      * @since    JDK1.0
  327.      */
  328.     public synchronized void setResizable(boolean resizable) {
  329.     this.resizable = resizable;
  330.         FramePeer peer = (FramePeer)this.peer;
  331.     if (peer != null) {
  332.         peer.setResizable(resizable);
  333.     }
  334.     }
  335.  
  336.     /**
  337.      * Removes the specified menu bar from this frame.
  338.      * @param    m   the menu component to remove.
  339.      * @since    JDK1.0
  340.      */
  341.     public void remove(MenuComponent m) {
  342.         synchronized (getTreeLock()) {
  343.         if (m == menuBar) {
  344.             menuBar = null;
  345.             FramePeer peer = (FramePeer)this.peer;
  346.             if (peer != null) {
  347.             mbManagement = true;
  348.             peer.setMenuBar(null);
  349.             m.removeNotify();
  350.             }
  351.          m.parent = null;
  352.         } else {
  353.                 super.remove(m);
  354.             }
  355.         }
  356.     }
  357.  
  358.     /**
  359.      * Disposes of the Frame. This method must
  360.      * be called to release the resources that
  361.      * are used for the frame.  All components
  362.      * contained by the frame and all windows
  363.      * owned by the frame will also be destroyed.
  364.      * @since JDK1.0
  365.      */
  366.     public void dispose() {     // synch removed.
  367.       synchronized (getTreeLock()) {
  368.     if (ownedWindows != null) {
  369.       int ownedWindowCount = ownedWindows.size();
  370.       Window ownedWindowCopy[] = new Window[ownedWindowCount];
  371.       ownedWindows.copyInto(ownedWindowCopy);
  372.       for (int i = 0; i < ownedWindowCount ; i++) {
  373.         ownedWindowCopy[i].dispose();
  374.       }
  375.     }
  376.     if (menuBar != null) {
  377.         remove(menuBar);
  378.         menuBar = null;
  379.     }
  380.       }
  381.       super.dispose();
  382.     }
  383.  
  384.     void postProcessKeyEvent(KeyEvent e) {
  385.         if (menuBar != null && menuBar.handleShortcut(e)) {
  386.             e.consume();
  387.             return;
  388.         }
  389.         super.postProcessKeyEvent(e);
  390.     }
  391.  
  392.     /**
  393.      * Returns the parameter String of this Frame.
  394.      */
  395.     protected String paramString() {
  396.     String str = super.paramString();
  397.     if (resizable) {
  398.         str += ",resizable";
  399.     }
  400.     if (title != null) {
  401.         str += ",title=" + title;
  402.     }
  403.     return str;
  404.     }
  405.  
  406.     /**
  407.      * @deprecated As of JDK version 1.1,
  408.      * replaced by <code>Component.setCursor(Cursor)</code>.
  409.      */
  410.     public synchronized void setCursor(int cursorType) {
  411.     if (cursorType < DEFAULT_CURSOR || cursorType > MOVE_CURSOR) {
  412.         throw new IllegalArgumentException("illegal cursor type");
  413.     }
  414.     setCursor(Cursor.getPredefinedCursor(cursorType));
  415.     }
  416.  
  417.     /**
  418.      * @deprecated As of JDK version 1.1,
  419.      * replaced by <code>Component.getCursor()</code>.
  420.      */
  421.     public int getCursorType() {
  422.     return (getCursor().getType());
  423.     }
  424.  
  425.  
  426.     /* Serialization support.  If there's a MenuBar we restore
  427.      * its (transient) parent field here.  Likewise for top level 
  428.      * windows that are "owned" by this frame.
  429.      */
  430.  
  431.     private int frameSerializedDataVersion = 1;
  432.  
  433.  
  434.     private void writeObject(ObjectOutputStream s)
  435.       throws IOException 
  436.     {
  437.       s.defaultWriteObject();
  438.     }
  439.  
  440.  
  441.     private void readObject(ObjectInputStream s)
  442.       throws ClassNotFoundException, IOException 
  443.     {
  444.       s.defaultReadObject();
  445.  
  446.       if (menuBar != null)
  447.     menuBar.parent = this;
  448.  
  449.       if (ownedWindows != null) {
  450.     for(int i = 0; i < ownedWindows.size(); i++) {
  451.       Window child = (Window)(ownedWindows.elementAt(i));
  452.       child.parent = this;
  453.     }
  454.       }
  455.     }
  456. }
  457.  
  458.